<?php

interface middleware {

  public static function handle(Closure $next);

}

//装饰器1

class MiddleStepOne implements middleware{

  public static function handle(Closure $next) {

    echo "步骤1前期处理的第一步".PHP_EOL;

    $next();

    echo "步骤1后期处理的第一步".PHP_EOL;

  }

}

//装饰器2

class MiddleStepTwo implements middleware{

  public static function handle(Closure $next) {

    echo "步骤2前期处理的第二步". PHP_EOL;

    $next();

    echo "步骤2后期处理的第二步". PHP_EOL;

  }

}

function goFunc() {

  return function ($step,$className) {

    return function () use ($step,$className) {

      return $className::handle($step);

    };

  };

}

$pip = array(
  MiddleStepOne::class,
  MiddleStepTwo::class,
);

$pip = array_reverse($pip);  //反转数组，以求达到要求的顺序运行

$first = function (){

  echo "前期处理完毕，将处理步骤2". PHP_EOL;

};  //实际要处理的函数

$a = array_reduce($pip,goFunc(),$first); //遍历pip数组，并将first作为第一个参数传递进去

$a(); //执行